package com.weixing.mall.provider.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.exception.BusinessException;
import com.weixing.mall.provider.mapper.AgentRebateRuleInfoMapper;
import com.weixing.mall.provider.mapper.AgentRebateRuleMapper;
import com.weixing.mall.provider.model.domain.AgentRebateRule;
import com.weixing.mall.provider.model.domain.AgentRebateRuleInfo;
import com.weixing.mall.provider.model.dto.AgentRebateRuleDto;
import com.weixing.mall.provider.model.dto.RuleInfoDto;
import com.weixing.mall.provider.model.query.AgentRebateRuleQuery;
import com.weixing.mall.provider.model.vo.AgentRebateRuleVo;
import com.weixing.mall.provider.model.vo.RebateRuleVo;
import com.weixing.mall.provider.service.IAgentRebateRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 促销模块-代理商活动返利规则配置表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-16
 */
@Service
public class AgentRebateRuleServiceImpl extends ServiceImpl<AgentRebateRuleMapper, AgentRebateRule> implements IAgentRebateRuleService {

    @Autowired
    private AgentRebateRuleInfoMapper agentRebateRuleInfoMapper;


    @Override
    public IPage<AgentRebateRuleVo> findList(Page initMpPage, AgentRebateRuleQuery query) {
        return baseMapper.findList(initMpPage,query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(AgentRebateRuleDto dto) {
        AgentRebateRule rebateRule = new AgentRebateRule();
        BeanUtils.copyProperties(dto,rebateRule);
        rebateRule.setIsEnable(dto.getIsEnable().getValue());
        if (null == dto.getId()){
            save(rebateRule);
        }else{
            updateById(rebateRule);
            // 先删除原来的详情；
            agentRebateRuleInfoMapper.deleteByRuleId(rebateRule.getId());
        }


        List<RuleInfoDto> ruleInfos = dto.getRuleInfos();
        AgentRebateRuleInfo info = null;
        for (RuleInfoDto rDto:ruleInfos){
            if (checkRebateRadio(rDto) > 1){
                throw new BusinessException("参数不合法,返利比例不能超过 1");
            }
            info = new AgentRebateRuleInfo();
            info.setRuleId(rebateRule.getId());
            info.setAgentLevel1(rDto.getAgentLevel1());
            info.setAgentLevel2(rDto.getAgentLevel2());
            info.setAgentLevel3(rDto.getAgentLevel3());
            info.setAgentLevel4(rDto.getAgentLevel4());
            info.setAgentLevel5(rDto.getAgentLevel5());
            info.setAgentLevel6(rDto.getAgentLevel6());
            info.setInfoCode(rDto.getInfoCode());
            info.setInfoRule(rDto.getRuleInfo());
            agentRebateRuleInfoMapper.insert(info);
        }
    }

    private double checkRebateRadio(RuleInfoDto dto){
        return NumberUtil.add(dto.getAgentLevel1(),dto.getAgentLevel2(),dto.getAgentLevel3(),dto.getAgentLevel4(),dto.getAgentLevel5(),dto.getAgentLevel6()).doubleValue();
    }

    @Override
    public void deleteById(long id) {
        removeById(id);
        agentRebateRuleInfoMapper.deleteByRuleId(id);
    }

    @Override
    public List<RebateRuleVo> getByName(String name) {
        return baseMapper.getByName(name);
    }

    @Override
    public List<RebateRuleVo> selectByRuleId(Long id) {
        return null;
    }

    @Override
    public AgentRebateRule selectByName(String ruleName) {
        return getOne(new QueryWrapper<AgentRebateRule>().eq("rule_name",ruleName));
    }
}
